package com.shiyi.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author wangzhanshuai
 * @date 2021/1/23 5:43 下午
 * @since 1.0
 */
@Controller
public class uploadController {

    @RequestMapping("/upload")
    public String upload(@RequestParam(value = "desc", required = false) String desc, @RequestParam("file") MultipartFile multipartFile) {
        if(multipartFile != null && !multipartFile.isEmpty()) {
            try {
                String filename = multipartFile.getOriginalFilename();
                System.out.println(filename);
                multipartFile.transferTo(new File("/Users/wangzhanshuai/project/spring-mvc-study/src/main/webapp/upload/" + filename));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "success";
    }

    @RequestMapping("/uploads")
    public String uploads(@RequestParam(value = "desc", required = false) String desc, @RequestParam("file") MultipartFile[] multipartFiles) {
        System.out.println(desc);
        if(multipartFiles != null) {
            for (MultipartFile multipartFile : multipartFiles) {
                if(multipartFile != null && !multipartFile.isEmpty()) {
                    try {
                        String filename = multipartFile.getOriginalFilename();
                        System.out.println(filename);
                        multipartFile.transferTo(new File("/Users/wangzhanshuai/project/spring-mvc-study/src/main/webapp/upload/" + filename));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return "success";
    }
}
